<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="社团名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['communityName', validatorRules.communityName]" placeholder="请输入社团名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="社团头像" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['communityLogo']" placeholder="请输入社团头像"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="社团简介" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['introduction']" placeholder="请输入社团简介"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="组织类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['organizationType']" placeholder="请输入组织类型"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="组织级别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['organizationLevel']" placeholder="请输入组织级别"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="社团群号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['communityNum']" placeholder="请输入社团群号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="社团证件图" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['communityPicture']" placeholder="请输入社团证件图"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="状态：1未认证，2已认证" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['type', validatorRules.type]" placeholder="请输入状态：1未认证，2已认证"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="审核：1待审核，2通过，3未通过" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['audit', validatorRules.audit]" placeholder="请输入审核：1待审核，2通过，3未通过"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="社团公告" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['clubAnnouncement']" placeholder="请输入社团公告"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="社团创建人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['communityCreateBy', validatorRules.communityCreateBy]" placeholder="请输入社团创建人"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="社团成员数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['numOfMembers']" placeholder="请输入社团成员数" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="档案类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['fileType']" placeholder="请输入档案类型"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="删除：1正常，2已删除" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['delFlag', validatorRules.delFlag]" placeholder="请输入删除：1正常，2已删除"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="社团等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['level', validatorRules.level]" placeholder="请输入社团等级"></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'

  export default {
    name: "CommunityManagementForm",
    components: {
      JFormContainer,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：false流程表单 true普通表单
      normal: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          communityName: {
            rules: [
              { required: true, message: '请输入社团名称!'},
            ]
          },
          type: {
            rules: [
              { required: true, message: '请输入状态：1未认证，2已认证!'},
            ]
          },
          audit: {
            rules: [
              { required: true, message: '请输入审核：1待审核，2通过，3未通过!'},
            ]
          },
          communityCreateBy: {
            rules: [
              { required: true, message: '请输入社团创建人!'},
            ]
          },
          delFlag: {
            rules: [
              { required: true, message: '请输入删除：1正常，2已删除!'},
            ]
          },
          level: {
            rules: [
              { required: true, message: '请输入社团等级!'},
            ]
          },
        },
        url: {
          add: "/communityManagement/communityManagement/add",
          edit: "/communityManagement/communityManagement/edit",
          queryById: "/communityManagement/communityManagement/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.normal===false){
          if(this.formData.disabled===false){
            return false
          }else{
            return true
          }
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.normal===false){
          if(this.formData.disabled===false){
            return true
          }else{
            return false
          }
        }else{
          return false
        }
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'communityName','communityLogo','introduction','organizationType','organizationLevel','communityNum','communityPicture','type','audit','clubAnnouncement','communityCreateBy','numOfMembers','fileType','delFlag','level'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.normal === false){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'communityName','communityLogo','introduction','organizationType','organizationLevel','communityNum','communityPicture','type','audit','clubAnnouncement','communityCreateBy','numOfMembers','fileType','delFlag','level'))
      },
    }
  }
</script>